package com.herohead.redis;

import java.io.IOException;

import redis.clients.jedis.Jedis;

import com.herohead.Misc;



/**
 * @author david-1
 *
 */

public class GetRedisServer {

	String localIP = Misc.getLocalHostIPaddress();

	public String redisHostName;


	public boolean checkForRedis(Jedis jd) throws IOException {

		final int numberOfRetries = 10 ;
		final long timeToWait = 30000 ;

		boolean redisGood = false;

		for (int i=1; i<numberOfRetries; i++) {

			try {
				jd.time();
				redisGood = true;
				break ;
			}
			catch (Exception e) {
				try {
					System.out.println("Trying to connect to redisServer " + redisHostName + " - try " + i + " of " + numberOfRetries + " - retry in 30 secs" );
					Thread.sleep(timeToWait);
				}
				catch (InterruptedException e1) {
				}
			}
		}


		return redisGood;
	}

	public String getHostName() {
		return redisHostName;
	}

	/**
	 * @return String Host for redis - if local IP address is 192.168.50.* then use rp-1 for server.
	 * 
	 */
	public String getRedisHost() {
		if (localIP.contains("192.168.50")) {
			redisHostName = "192.168.50.12";
		} else if (localIP.contains("169.254")) {
			redisHostName = "192.168.50.12";
		}
		else {
			redisHostName = "172.18.38.14";
		}
		RedisVars.redisHost = redisHostName;
		return redisHostName;
	}


	public void setHostName(String redisHost) {
		redisHostName = redisHost;
	}

}
